#! /usr/bin/python
# -*- coding:utf-8 -*-

## ========================================================================
## Copyright (C) 2007-2013  Jean-Philippe GOLAY
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## ========================================================================

import os
import cgi
from time import strftime
from backup import *

form = cgi.FieldStorage()
msg=""

print u"Content-type: text/plain"
print

if form.has_key(u"session"): 
  session = form[u"session"].value

bk=Backup()


table=bk.userTable()
if table == 0:
  msg += u"<br/>Aucunes donn&eacute;es pour la table cms_user !"
else:
  content_file = "# BACKUP COMICS DATABASE\n# TABLE cms_user\n# DATE %s\n\n" % strftime("%Y-%m-%d %H:%M:%S")
  content_file += Glob.dicTables['cms_user']
  for row in table:
    content_file += "INSERT INTO `cms_user` (`id`, `user_name`, `user_password`) VALUES (%s, '%s', '%s');\n" % ( row[0], row[1], row[2] )
  try:
    sql_file = open( Glob.SITE_PATH + u"backup/user_table.sql", 'w')
    sql_file.write(content_file)
    sql_file.close()
    msg += u"<br/>Fichier user_table.sql enregistr&eacute; avec succ&egrave;s !"
  except error:
    msg += u"<br/>Error impossible d'enregistrer le fichier user_table.sql !\n%s" % error


table=bk.navTable()
if table == 0:
  msg += u"<br/>Aucunes donn&eacute;es pour la table cms_nav !"
else:
  content_file = "# BACKUP COMICS DATABASE\n# TABLE cms_nav\n# DATE %s\n\n" % strftime("%Y-%m-%d %H:%M:%S")
  content_file += Glob.dicTables['cms_nav']
  for row in table:
    content_file += "INSERT INTO `cms_nav` (`id`, `nav_name`, `nav_title`, `nav_parent_id`, `nav_position`, `nav_onclick`, `nav_selected`, `page_id`) VALUES (%s, '%s', '%s', %s, %s, '%s', %s, %s );\n" % ( row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7] )
  try:
    sql_file = open( Glob.SITE_PATH + u"backup/nav_table.sql", 'w')
    sql_file.write(content_file)
    sql_file.close()
    msg += u"<br/>Fichier nav_table.sql enregistr&eacute; avec succ&egrave;s !"
  except error:
    msg += u"<br/>Error impossible d'enregistrer le fichier nav_table.sql !\n%s" % error


table=bk.pageTable()
if table == 0:
  msg += u"<br/>Aucunes donn&eacute;es pour la table cms_page !"
else:
  content_file = "# BACKUP COMICS DATABASE\n# TABLE cms_page\n# DATE %s\n\n" % strftime("%Y-%m-%d %H:%M:%S")
  content_file += Glob.dicTables['cms_page']
  for row in table:
    content_file += "INSERT INTO `cms_page` (`id`, `page_title`, `page_content`, `nav_id`) VALUES (%s, '%s', '%s', %s);\n" % ( row[0], row[1].replace('"', '\\"').replace("'", "\\'"), row[2].replace('"', '\\"').replace("'", "\\'"), row[3] )
  try:
    sql_file = open( Glob.SITE_PATH + u"backup/page_table.sql", 'w')
    sql_file.write(content_file)
    sql_file.close()
    msg += u"<br/>Fichier page_table.sql enregistr&eacute; avec succ&egrave;s !"
  except error:
    msg += u"<br/>Error impossible d'enregistrer le fichier page_table.sql !\n%s" % error


table=bk.configTable()
if table == 0:
  msg += u"<br/>Aucunes donn&eacute;es pour la table cms_config !"
else:
  content_file = "# BACKUP COMICS DATABASE\n# TABLE cms_config\n# DATE %s\n\n" % strftime("%Y-%m-%d %H:%M:%S")
  content_file += Glob.dicTables['cms_config']
  for row in table:
    content_file += "INSERT INTO `cms_config` (`id`, `config_title`, `config_pub`, `config_description`, `config_keyword`, `config_headtag`, `config_url`, `config_email`, `config_skin`, `config_language`, `config_name`) VALUES (%s, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');\n" % ( row[0], row[1].replace('"', '\\"').replace("'", "\\'"), row[2].replace('"', '\\"').replace("'", "\\'"), row[3].replace('"', '\\"').replace("'", "\\'"), row[4].replace('"', '\\"').replace("'", "\\'"), row[5].replace('"', '\\"').replace("'", "\\'"), row[6], row[7], row[8], row[9], row[10] )
  try:
    sql_file = open( Glob.SITE_PATH + u"backup/config_table.sql", 'w')
    sql_file.write(content_file)
    sql_file.close()
    msg += u"<br/>Fichier config_table.sql enregistr&eacute; avec succ&egrave;s !"
  except error:
    msg += u"<br/>Error impossible d'enregistrer le fichier config_table.sql !\n%s" % error


table=bk.sessionTable()
if table == 0:
  msg += u"<br/>Aucunes donn&eacute;es pour la table cms_session !"
else:
  content_file = "# BACKUP COMICS DATABASE\n# TABLE cms_session\n# DATE %s\n\n" % strftime("%Y-%m-%d %H:%M:%S")
  content_file += Glob.dicTables['cms_session']
  for row in table:
    if row[3] == None:
      user_id='NULL'
    else:
      user_id=row[3]
    content_file += "INSERT INTO `cms_session` (`id`, `ses_ip`, `ses_date`, `user_id`) VALUES (%s, '%s', '%s', %s);\n" % ( row[0], row[1], row[2], user_id )
  try:
    sql_file = open( Glob.SITE_PATH + u"backup/session_table.sql", 'w')
    sql_file.write(content_file)
    sql_file.close()
    msg += u"<br/>Fichier session_table.sql enregistr&eacute; avec succ&egrave;s !"
  except error:
    msg += u"<br/>Error impossible d'enregistrer le fichier session_table.sql !\n%s" % error
  

table=bk.saleTable()
if table == 0:
  msg += u"<br/>Aucunes donn&eacute;es pour la table cms_sale !"
else:
  content_file = "# BACKUP COMICS DATABASE\n# TABLE cms_sale\n# DATE %s\n\n" % strftime("%Y-%m-%d %H:%M:%S")
  content_file += Glob.dicTables['cms_sale']
  for row in table:
    content_file += "INSERT INTO `cms_sale` (`id`, `session_id`, `sale_date`, `sale_content`, `sale_price`, `sale_name`, `sale_addr`, `sale_email`, `sale_phone`, `sale_note`, `sale_pay`, `sale_date_pay`) VALUES (%s, %s, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');\n" % ( row[0], row[1], row[2], row[3].replace('"', '\\"').replace("'", "\\'"), row[4], row[5].replace('"', '\\"').replace("'", "\\'"), row[6].replace('"', '\\"').replace("'", "\\'"), row[7], row[8], row[9].replace('"', '\\"').replace("'", "\\'"), row[10], row[11] )
  try:
    sql_file = open( Glob.SITE_PATH + u"backup/sale_table.sql", 'w')
    sql_file.write(content_file)
    sql_file.close()
    msg += u"<br/>Fichier sale_table.sql enregistr&eacute; avec succ&egrave;s !"
  except error:
    msg += u"<br/>Error impossible d'enregistrer le fichier sale_table.sql !\n%s" % error


table=bk.productTable()
if table == 0:
  msg += u"<br/>Aucunes donn&eacute;es pour la table cms_product !"
else:
  content_file = "# BACKUP COMICS DATABASE\n# TABLE cms_product\n# DATE %s\n\n" % strftime("%Y-%m-%d %H:%M:%S")
  content_file += Glob.dicTables['cms_product']
  for row in table:
    content_file += "INSERT INTO `cms_product` (`id`, `prod_ref`, `prod_desig`, `prod_desc`, `prod_option`, `prod_img`, `prod_price`, `prod_tax`, `prod_dispo`, `prod_stock`) VALUES (%s, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');\n" % ( row[0], row[1], row[2].replace('"', '\\"').replace("'", "\\'"), row[3].replace('"', '\\"').replace("'", "\\'"), row[4].replace('"', '\\"').replace("'", "\\'"), row[5], row[6], row[7], row[8], row[9] )
  try:
    sql_file = open( Glob.SITE_PATH + u"backup/product_table.sql", 'w')
    sql_file.write(content_file)
    sql_file.close()
    msg += u"<br/>Fichier product_table.sql enregistr&eacute; avec succ&egrave;s !"
  except error:
    msg += u"<br/>Error impossible d'enregistrer le fichier product_table.sql !\n%s" % error


table=bk.helpTable()
if table == 0:
  msg += u"<br/>Aucunes donn&eacute;es pour la table cms_help !"
else:
  content_file = "# BACKUP COMICS DATABASE\n# TABLE cms_help\n# DATE %s\n\n" % strftime("%Y-%m-%d %H:%M:%S")
  content_file += Glob.dicTables['cms_help']
  for row in table:
    content_file += "INSERT INTO `cms_help` (`id`, `help_content`) VALUES (%s, '%s');\n" % ( row[0], row[1].replace('"', '\\"').replace("'", "\\'") )
  try:
    sql_file = open( Glob.SITE_PATH + u"backup/help_table.sql", 'w')
    sql_file.write(content_file)
    sql_file.close()
    msg += u"<br/>Fichier help_table.sql enregistr&eacute; avec succ&egrave;s !"
  except error:
    msg += u"<br/>Error impossible d'enregistrer le fichier help_table.sql !\n%s" % error

    
print msg
